home *** CD-ROM | disk | FTP | other *** search
/ Power CD / Power CD ATARI-Rechner Lieben.iso / SPEZIAL / GEMVIEW / VERSION.3 / MODULS.SRC / SAVEMODL / ESM / ESM.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-15  |  5.6 KB  |  195 lines

  1. /* GEM-View 3.00 SAVING Modul: Bild als Enhanced Simplex speichern
  2.    (c) 22.10.1993 by Dieter Fiebelkorn
  3.                      GrĂ¼ner Weg 29a
  4.                      D-45768 Marl
  5.                      (GERMANY)
  6. */
  7.  
  8. #include <stdio.h>
  9. #include "image.h"
  10. #include "moduls.h"
  11. #include "esm.h"
  12.  
  13. #define Write(a,b,c,d)  write(d,c,a)
  14.  
  15.  
  16.  
  17. void Save_esm0 (SAVE_Structure *saveS, unsigned int verbose, unsigned int  truecolor)
  18. {
  19.   Image          *image;
  20.   char           *filename;
  21.   unsigned int    windepth;
  22.   int             fd;
  23.   ESM_Header      esm;
  24.   char           *data, out,
  25.                   mask, bbuf[4];
  26.   long            col, line;
  27.   unsigned char  *raster[8];
  28.   long            width, height,
  29.                   RealLine;
  30.   short           planes, plane, i,
  31.                   buf[4] = {0, 0, 0, 0};
  32.  
  33.   image      = saveS->image;
  34.   filename   = saveS->out_filename;
  35.   windepth   = saveS->screen_depth;
  36.  
  37.   if ((width = image->unalignwidth) == 0)
  38.     width      = image->width;
  39.   height     = image->height;
  40.   planes     = image->depth;
  41.   
  42.   *(long*)&esm.kennung = 'TMS\0';
  43.   esm.head_size     = sizeof(ESM_Header) + 4 * sizeof(short);
  44.   esm.breite        = width;
  45.   esm.hoehe         = height;
  46.   if (MONOCHROMEP(image)) {
  47.     truecolor       = FALSE;
  48.     esm.tiefe       = 1;
  49.     esm.bildart     = 1;
  50.   }
  51.   if (PALETTEP(image) && !truecolor) {
  52.     esm.tiefe       = 8;
  53.     esm.bildart     = 3;
  54.   }
  55.   if (TRUECOLORP(image) || truecolor) {
  56.     esm.tiefe       = 24;
  57.     esm.bildart     = 3;
  58.   }
  59.   esm.tiefe_rot     = 8;
  60.   esm.tiefe_gruen   = 8;
  61.   esm.tiefe_blau    = 8;
  62.   esm.tiefe_schwarz = 0;
  63.   esm.version       = 4;
  64.   esm.xdpi          = 100;
  65.   esm.ydpi          = 100;
  66.   esm.file_hoehe    = height;
  67.   esm.start_zeile   = 1;
  68.   esm.end_zeile     = height;
  69.   esm.maske         = 0;
  70.   for (col = 0; col < 256; col++)
  71.     esm.rot_tab  [col] = esm.gruen_tab[col] = esm.blau_tab [col] = col;
  72.   if (!truecolor)
  73.     for (col = 0; col < image->rgb.used; col++) {
  74.       esm.rot_tab  [col] = (char)(image->rgb.red  [col] >> 8);
  75.       esm.gruen_tab[col] = (char)(image->rgb.green[col] >> 8);
  76.       esm.blau_tab [col] = (char)(image->rgb.blue [col] >> 8);
  77.     }
  78.   
  79.   saveS->print.printout ("  Saving Enhanced-SiMplex Image '%s' ... ", filename);
  80.  
  81.   if ((fd = saveS->output.open(filename)) > 0) {
  82.     if (saveS->output.Write(&esm, 1L, sizeof(ESM_Header), fd) != sizeof(ESM_Header))
  83.       goto CloseAndRemove;
  84.     if (saveS->output.Write(&buf, 1L, 4L * sizeof(short), fd) != 4L * sizeof(short))
  85.       goto CloseAndRemove;
  86.  
  87.  
  88.     if (MONOCHROMEP(image)) {
  89.       width = (width >> 3) + (width & 0x0007 ? 1 : 0);
  90.       width <<= 3;
  91.       RealLine = (width >> 4) + ((width & 0x000F) ? 1 : 0);
  92.       RealLine *= 2;
  93.       data = image->data;
  94.       for (line = 0; line < height; line++) {
  95.         if (saveS->output.Write(data, 1L, width / 8, fd) != width / 8)
  96.           goto CloseAndRemove;
  97.         data += RealLine;
  98.       }
  99.     }
  100.     
  101.     if (ATARI_RGBP(image)) {
  102.       RealLine = (width >> 4) + ((width & 0x000F) ? 1 : 0);
  103.       RealLine *= 2;
  104.       for (line = 0; line < height; line++) {
  105.         raster[0] = image->data + ((long)line * RealLine);
  106.         for (plane = 1; plane < planes; plane++)
  107.           raster[plane] = raster[plane-1] + (long)RealLine * height;
  108.  
  109.         mask = 0x80;
  110.         col = 0;
  111.         for (i = 0; i < width; i++) {
  112.           out = 0;
  113.           for (plane = 0; plane < planes; plane++) {
  114.             if (raster[plane][col] & mask)
  115.               out |= (1U << plane);
  116.           }
  117.           mask >>= 1;
  118.           if (!truecolor) {
  119.             if (saveS->output.Write(&out, 1L, 1L, fd) != 1L)
  120.               goto CloseAndRemove;
  121.           } else {
  122.             bbuf[0] = (char)(image->rgb.red  [out] >> 8);
  123.             bbuf[1] = (char)(image->rgb.green[out] >> 8);
  124.             bbuf[2] = (char)(image->rgb.blue [out] >> 8);
  125.             if (saveS->output.Write(bbuf, 1L, 3L, fd) != 3L)
  126.               goto CloseAndRemove;
  127.           }
  128.  
  129.           if (mask == 0) {
  130.             mask = 0x80;
  131.             col++;
  132.           }
  133.         }
  134.       }
  135.     }
  136.  
  137.     if (RGBP(image)) {
  138.       RealLine = (width >> 4) + ((width & 0x000F) ? 1 : 0);
  139.       RealLine <<= 4;
  140.       data = image->data;
  141.       for (line = 0; line < height; line++) {
  142.         if (!truecolor) {
  143.           if (saveS->output.Write(data, 1L, width, fd) != width)
  144.             goto CloseAndRemove;
  145.         } else {
  146.           for (i = 0; i < width; i++) {
  147.             out = data[i];
  148.             bbuf[0] = (char)(image->rgb.red  [out] >> 8);
  149.             bbuf[1] = (char)(image->rgb.green[out] >> 8);
  150.             bbuf[2] = (char)(image->rgb.blue [out] >> 8);
  151.             if (saveS->output.Write(bbuf, 1L, 3L, fd) != 3L)
  152.               goto CloseAndRemove;
  153.           }
  154.         }
  155.         data += RealLine;
  156.       }
  157.     }
  158.  
  159.     if (TRUECOLORP(image)) {
  160.       RealLine = (width >> 4) + ((width & 0x000F) ? 1 : 0);
  161.       RealLine <<= 4;
  162.       RealLine *= 3;
  163.       data = image->data;
  164.       for (line = 0; line < height; line++) {
  165.         if (saveS->output.Write(data, 1L, 3L * width, fd) != 3L * width)
  166.           goto CloseAndRemove;
  167.         data += RealLine;
  168.       }
  169.     }
  170.  
  171.  
  172.     if (saveS->output.close(fd) != 0) {
  173.       fd = 0;
  174.       goto CloseAndRemove;
  175.     }
  176.     saveS->print.printout ("done.\n");
  177.     return;
  178.   }
  179.   
  180. CloseAndRemove:
  181.   saveS->events.alert(1, "[3][ | Error in writing!  | |    Disk full ? ][   OK   ]");
  182.   if (fd > 0)
  183.     saveS->output.close(fd);
  184.   saveS->output.delete(filename);
  185.   saveS->print.printout ("error.\n");
  186.   return;
  187. }
  188.  
  189.  
  190.  
  191. void Save_esm (SAVE_Structure *saveS, unsigned int verbose)
  192. {
  193.   Save_esm0 (saveS, verbose, FALSE);
  194. }
  195.